﻿// Decompiled with JetBrains decompiler
// Type: MCM.LightInject.ImmutableHashTable`2
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using System;
using System.Diagnostics.CodeAnalysis;

#nullable disable
namespace MCM.LightInject
{
  [ExcludeFromCodeCoverage]
  internal sealed class ImmutableHashTable<TKey, TValue>
  {
    public static readonly ImmutableHashTable<TKey, TValue> Empty = new ImmutableHashTable<TKey, TValue>();
    public readonly int Count;
    internal readonly ImmutableHashTree<TKey, TValue>[] Buckets;
    internal readonly int Divisor;

    internal ImmutableHashTable(ImmutableHashTable<TKey, TValue> previous, TKey key, TValue value)
    {
      this.Count = previous.Count + 1;
      if (previous.Count >= previous.Divisor)
      {
        this.Divisor = previous.Divisor * 2;
        this.Buckets = new ImmutableHashTree<TKey, TValue>[this.Divisor];
        this.InitializeBuckets(0, this.Divisor);
        this.AddExistingValues(previous);
      }
      else
      {
        this.Divisor = previous.Divisor;
        this.Buckets = new ImmutableHashTree<TKey, TValue>[this.Divisor];
        Array.Copy((Array) previous.Buckets, (Array) this.Buckets, previous.Divisor);
      }
      int index = key.GetHashCode() & this.Divisor - 1;
      this.Buckets[index] = this.Buckets[index].Add<TKey, TValue>(key, value);
    }

    private ImmutableHashTable()
    {
      this.Buckets = new ImmutableHashTree<TKey, TValue>[2];
      this.Divisor = 2;
      this.InitializeBuckets(0, 2);
    }

    private void AddExistingValues(ImmutableHashTable<TKey, TValue> previous)
    {
      foreach (ImmutableHashTree<TKey, TValue> bucket in previous.Buckets)
      {
        foreach (KeyValue<TKey, TValue> keyValue in bucket.InOrder<TKey, TValue>())
        {
          int index = keyValue.Key.GetHashCode() & this.Divisor - 1;
          this.Buckets[index] = this.Buckets[index].Add<TKey, TValue>(keyValue.Key, keyValue.Value);
        }
      }
    }

    private void InitializeBuckets(int startIndex, int count)
    {
      for (int index = startIndex; index < count; ++index)
        this.Buckets[index] = ImmutableHashTree<TKey, TValue>.Empty;
    }
  }
}
